package cn.wzptsoft.mall.handler;


import cn.wzptsoft.mall.pojo.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(RuntimeException ex){
        log.error("异常信息：{}", ex.getMessage());
        System.out.println("----**-**-**-**-**---"+ex);
        if (ex instanceof RedisConnectionFailureException){
            return Result.error(-1,"服务器缓存异常，请联系管理员");
        }
        return Result.error(-1,"全局异常处理器："+ex.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Result AccessDeniedException(AccessDeniedException ex){
        log.error("异常信息：{}", ex.getMessage());
        System.out.println("----**-**-**-**-**---"+ex);
        return Result.error(-1,"全局异常处理器："+ex.getMessage()+"，无权限");
    }

}
